/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tcardrecordinfo.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.mob.common.DataConvertHelper;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.tcardrecordinfo.entity.TCardcecordInfo;
import com.jeeplus.modules.tcardrecordinfo.service.TCardcecordInfoService;

/**
 * 打卡记录Controller
 * @author pr
 * @version 2022-04-22
 */
@RestController
@RequestMapping(value = "/tcardrecordinfo/tCardcecordInfo")
public class TCardcecordInfoController extends BaseController {

	@Autowired
	private TCardcecordInfoService tCardcecordInfoService;

	@ModelAttribute
	public TCardcecordInfo get(@RequestParam(required=false) String id) {
		TCardcecordInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tCardcecordInfoService.get(id);
		}
		if (entity == null){
			entity = new TCardcecordInfo();
		}
		return entity;
	}

	/**
	 * 打卡记录列表列表数据
	 */
	@RequiresPermissions("tcardrecordinfo:tCardcecordInfo:list")
	@GetMapping("list")
	public AjaxJson list(TCardcecordInfo tCardcecordInfo, HttpServletRequest request, HttpServletResponse response) {
		Page<TCardcecordInfo> page = tCardcecordInfoService.findPage(new Page<TCardcecordInfo>(request, response), tCardcecordInfo);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取打卡记录列表数据
	 */
	@RequiresPermissions(value={"tcardrecordinfo:tCardcecordInfo:view","tcardrecordinfo:tCardcecordInfo:add","tcardrecordinfo:tCardcecordInfo:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(TCardcecordInfo tCardcecordInfo) {
		return AjaxJson.success().put("tCardcecordInfo", tCardcecordInfo);
	}

	/**
	 * 保存打卡记录列表
	 */
	@RequiresPermissions(value={"tcardrecordinfo:tCardcecordInfo:add","tcardrecordinfo:tCardcecordInfo:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(TCardcecordInfo tCardcecordInfo, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(tCardcecordInfo);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		tCardcecordInfoService.save(tCardcecordInfo);//保存
		return AjaxJson.success("保存打卡记录列表成功");
	}


	/**
	 * 批量删除打卡记录列表
	 */
	@RequiresPermissions("tcardrecordinfo:tCardcecordInfo:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			tCardcecordInfoService.delete(new TCardcecordInfo(id));
		}
		return AjaxJson.success("删除打卡记录列表成功");
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("tcardrecordinfo:tCardcecordInfo:export")
    @GetMapping("export")
    public AjaxJson exportFile(TCardcecordInfo tCardcecordInfo, HttpServletRequest request, HttpServletResponse response) {
		try {
            String fileName = "打卡记录列表"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<TCardcecordInfo> page = tCardcecordInfoService.findPage(new Page<TCardcecordInfo>(request, response, -1), tCardcecordInfo);
    		new ExportExcel("打卡记录列表", TCardcecordInfo.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error("导出打卡记录列表记录失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("tcardrecordinfo:tCardcecordInfo:import")
    @PostMapping("import")
   	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<TCardcecordInfo> list = ei.getDataList(TCardcecordInfo.class);
			for (TCardcecordInfo tCardcecordInfo : list){
				try{
					tCardcecordInfoService.save(tCardcecordInfo);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条打卡记录列表记录。");
			}
			return AjaxJson.success( "已成功导入 "+successNum+" 条打卡记录列表记录"+failureMsg);
		} catch (Exception e) {
			return AjaxJson.error("导入打卡记录列表失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 下载导入打卡记录列表数据模板
	 */
	@RequiresPermissions("tcardrecordinfo:tCardcecordInfo:import")
    @GetMapping("import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		try {
            String fileName = "打卡记录列表数据导入模板.xlsx";
    		List<TCardcecordInfo> list = Lists.newArrayList();
    		new ExportExcel("打卡记录列表数据", TCardcecordInfo.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error( "导入模板下载失败！失败信息："+e.getMessage());
		}
    }

    //打卡审核
    @PostMapping("audit")
    public AjaxJson audit(@RequestParam("id") String id,@RequestParam("punchType") String type,@RequestParam("auditStatus") String status) {
		AjaxJson ajaxJson = tCardcecordInfoService.audit(id, type, status);
		return ajaxJson;
	}

	// 今日总打卡数
	@GetMapping("cardCount")
	public AjaxJson cardCount() {
		AjaxJson ajaxJson = tCardcecordInfoService.cardCount();
		return ajaxJson;
	}

	// 今日在途打卡总次数
	@GetMapping("wayCardCount")
	public AjaxJson wayCardCount(HttpServletRequest request) {
		Map<String, Object> params = DataConvertHelper.getRequestParams(request);
		AjaxJson ajaxJson = tCardcecordInfoService.wayCardCount(params.get("punchType").toString());
		return ajaxJson;
	}

	// 今日楼宇打卡总次数
	@GetMapping("buildingCardCount")
	public AjaxJson buildingCardCount(HttpServletRequest request) {
		Map<String, Object> params = DataConvertHelper.getRequestParams(request);
		AjaxJson ajaxJson = tCardcecordInfoService.buildingCardCount(params.get("punchType").toString());
		return ajaxJson;
	}

	// 打卡标记图
	@GetMapping("queryAllMap")
	public AjaxJson queryAllMap(HttpServletRequest request) {
		AjaxJson ajaxJson = tCardcecordInfoService.queryAllMap(request);
		return ajaxJson;
	}


}